﻿using System;
using System.Threading;
using System.Configuration;
using DataAccess;

namespace Spider
{
    /// <summary>
    /// 电影天堂
    /// </summary>
    namespace Dygod
    {
        /// <summary>
        /// 爬虫Handle
        /// </summary>
        public class Handle
        {
            /// <summary>
            /// 是否正在运行
            /// </summary>
            private bool IsRunning;

            /// <summary>
            /// handle的处理线程
            /// </summary>
            private Thread Thread;

            /// <summary>
            /// 开始爬虫
            /// </summary>
            public void Start()
            {
                Thread = new Thread(new ThreadStart(GrabThread));
                Thread.Start();
            }

            /// <summary>
            /// 结束爬虫
            /// </summary>
            public void Stop()
            {
                IsRunning = false;
            }

            /// <summary>
            /// 抓取线程
            /// </summary>
            private void GrabThread()
            {
                while (IsRunning)
                {
                    int period = int.Parse(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["period"].Value);
                    try
                    {
                        new Home().DoWork();
                        new MovieHome().DoWork();
                        new TVHome().DoWork();
                        Public.PostBaidu("https://www.bluegod.xyz", "https://www.bluegod.xyz", "DUZfwlBkyUAhcBrH");
                    }
                    catch (Exception e)
                    {
                        Public.WriteLog("Dygod Handle Exception: " + e.Message);
                    }
                    Public.WriteLog("~~~~~ Dygod Handle Sleep " + period.ToString() + " Mins ~~~~~");
                    Thread.Sleep(period * 1000 * 60);
                }
            }
        }
    }
}